/*
 * @Author: your name
 * @Date: 2021-05-31 14:25:34
 * @LastEditTime: 2021-06-03 17:15:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_island_api\app\api\index.js
 */
const Router = require('koa-router')
const router = new Router()

router.prefix('/v1/classic')

// const {PositiveIntegerValidator} = require('../../validators/validators')
// 别名 alias
const { Auth } = require('@middlewares/auth')
const { Flow } = require('@models/flow')
const { Art } = require('@models/art')
const { Favor } = require('@models/favor')
const { PositiveIntegerValidator, ClassicValidator } = require('@validators/validators')

router.get('/latest',new Auth().m, async(ctx, next) => {
  index = 8
  // 排序 升序1, 2, 3, 4, 5,...max
  // 降序max...5,4,3
  const flow = await Flow.findOne({
    // 倒叙排序
    order: [
      ['index', 'DESC']
    ]
  })
  const art = await Art.getData(flow.art_id, flow.type)
  // 序列化
  // art.dataValues.index = flow.index
  const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', likeLatest)
  ctx.body = {
    code: 200,
    data: art
  }
  // koa
  // 序列化 对象  json
})
// 下一期期刊
router.get('/:index/next', new Auth().m, async(ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
    where: {
      index: index + 1
    }
  })
  if (!flow) {
    throw new global.errs.NotFound()
  }
  const art = await Art.getData(flow.art_id, flow.type)
  // 序列化
  // art.dataValues.index = flow.index
  const likeNext = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', likeNext)
  ctx.body = {
    code: 200,
    data: art
  }
})

// 上一期
router.get('/:index/previous', new Auth().m, async(ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
    id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
    where: {
      index: index - 1
    }
  })
  if (!flow) {
    throw new global.errs.NotFound()
  }
  const art = await Art.getData(flow.art_id, flow.type)
  // 序列化
  // art.dataValues.index = flow.index
  const likePrevious = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', likePrevious)
  ctx.body = {
    code: 200,
    data: art
  }
})

// 获取期刊点赞信息
router.get('/:type/:id/favor', new Auth().m, async(ctx) => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))
  const artDetail = await new Art(id, type).getDetail(ctx.auth.uid)
  
  ctx.body = {
    code: 200,
    data: {
      fav_nums: artDetail.art.fav_nums,
      like_status: artDetail.like_status
    }
  }
})

// 获取点赞信息
router.get('/favor', new Auth().m, async (ctx) => {
  const uid = ctx.auth.uid
  ctx.body = await Favor.getMyClassicFavors(uid)
})

router.get('/:type/:id', new Auth().m, async(ctx) => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))

  const artDetail = await new Art(id, type).getDetail(ctx.auth.uid)
  
  ctx.body = {
    code: 200,
    data: { ...artDetail }
  }
})

module.exports = router
